﻿using Shumei.EFCore.SimpleService;
using source.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace source.AttributeExtend
{
    public class UserLoginAuth : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (AccountController.isLogin(httpContext))
            {
                var user = AccountController.GetUserInfo(httpContext);
                var roleNum = int.Parse(Roles);
                if (user.Role < roleNum)
                    return false;
                else
                    return true;
            }
              
            return false;
            //return base.AuthorizeCore(httpContext);
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);
            var Url = new UrlHelper(filterContext.RequestContext);
            var url = Url.Action("Login", "Account");
            //转去登录的地址
            filterContext.Result = new RedirectResult(url);
        }
    }
}